Overview
Duet uses Cloudflare’s AI service with the Llama 3 8B Instruct model to provide intelligent pair programming assistance. The LLM generates responses based on conversation history and can output commands for sandbox execution.Model Configuration
The worker uses Cloudflare AI binding configured inwrangler.toml:
~/workspace/source/cf-worker/wrangler.toml:12-13
The model is accessed via the @cf/meta/llama-3-8b-instruct identifier:
~/workspace/source/cf-worker/index.ts:122-127
Message Format
The AI expects messages in a specific format:~/workspace/source/cf-worker/index.ts:84-87
System Prompt
Duet’s AI behavior is defined by a system prompt that instructs it to be concise and use special tags for command execution:~/workspace/source/cf-worker/index.ts:154-162
This prompt establishes:
- Identity: Duet is a pair-programming assistant
- Tone: Concise and action-oriented
- Command syntax: Use
<run>command</run>tags - Output handling: Don’t predict output, let sandbox provide it
Conversation Context
The AI receives context from recent conversation history:~/workspace/source/cf-worker/index.ts:154-168
The agent:
- Includes the system prompt
- Adds the last 10 messages from conversation history
- Appends the current user message
- Converts message roles (“agent” → “assistant”, “user” → “user”)
Command Execution Flow
The AI can trigger sandbox commands using special tags:1. AI Response with Commands
The AI wraps commands in<run> tags:
2. Command Extraction
The agent extracts commands using regex:~/workspace/source/cf-worker/index.ts:185-193
3. Sandbox Execution
Each extracted command is executed in the room’s sandbox:~/workspace/source/cf-worker/index.ts:195-205
4. Output Appending
Command outputs are appended to the AI’s response:- First 500 characters of stdout or stderr
- Error messages if execution fails
- “[no output]” if command produces nothing
5. Tag Removal
The<run> tags are stripped from the final response:
~/workspace/source/cf-worker/index.ts:207
Complete Message Flow
~/workspace/source/cf-worker/index.ts:129-183
API Request/Response
Request Format
~/workspace/source/cf-worker/index.ts:9-12
Response Format
~/workspace/source/internal/ai/client.go:42-47
Next Steps
- Sandboxes - Learn how commands are executed safely
- Durable Objects - Understand conversation state management
- Cloudflare Workers - See the request routing layer